package com.ys.controller.web.hr;

import com.ys.config.MenuId;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.entity.sys.YsMenuNo;
import com.ys.mapper.sys.YsMenuNoMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/web/sys/thrOperateCompanyBankInfo")
public class WebThrOperateCompanyBankInfoController {
	

	@Autowired
	private CommonApiService<ThrOperateCompanyBankInfo> commonApiService;

	@Autowired
	private YsMenuNoMapper ysMenuNoMapper;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String vid = "";
			YsMenuNo entity = new YsMenuNo().setMenuId(MenuId.THR_OperateCompanyBankInfo).queryFirst();
			if (entity == null) {
				throw new Exception("尚未设置单号格式");
			} else {
				Integer currentNo = entity.getCurrentNo();
				String connectCode = entity.getConnectCode();
				String preCode = entity.getPreCode();
				// 序号+1
				entity.setCurrentNo(currentNo + 1);
				ysMenuNoMapper.update(entity);
				String currentNoNew = String.format("%04d",currentNo);
				vid =  preCode + connectCode + currentNoNew;
			}

			commonApiService.toAdd(model, new ThrOperateCompanyBankInfo().setStatus(1).setVid(vid).setStatus(1), MenuId.THR_OperateCompanyBankInfo, null, null);
			return "hr/thrOperateCompanyBankInfo/thrOperateCompanyBankInfoAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("id") == null){
			return W.error(model, "id不能为空");
		}

		Long id = Long.valueOf((params.get("id").toString()));

		Map entity = new ThrOperateCompanyBankInfo().setId(id)
				.addLeftJoin("THR_OperateCompanyApply B ON  B.vgsbm = A.vgsbm")
				.addLeftJoin("TS_OperateOrg C on C.Id = B.OperateOrgId")
				.addField("C.Name operateOrgName")
				.addField("B.vgsbm AS vgsbm,B.vgsjc AS vgsjc")
				.queryFirstMap();
		if (entity == null){
			return W.error(model, "运营公司银行账户不存在!");
		}

		try {
			commonApiService.toEdit(model, id, new ThrOperateCompanyBankInfo().setId(id), MenuId.THR_OperateCompanyBankInfo, entity, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "hr/thrOperateCompanyBankInfo/thrOperateCompanyBankInfoEdit";
	}

}
